package com.ysg.widget.pic;

import android.content.Context;
import androidx.fragment.app.FragmentActivity;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.config.SelectModeConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnExternalPreviewEventListener;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YFileUtil;
import com.ysg.utils.YPermissionUtil;
import com.ysg.utils.YToastUtil;
import com.ysg.widget.pic.watermark.WatermarkEntity;
import java.util.ArrayList;
import java.util.List;

public class YPicUtils {
    public interface OnResultListener{
        void onResultListener(ArrayList<LocalMedia> result);
    }

    private static YPicUtils instance = null;

    public static YPicUtils getInstance(){
        if (instance == null){
            synchronized (YPicUtils.class){
                if (instance == null){
                    instance = new YPicUtils();
                }
            }
        }
        return instance;
    }

    public YPicUtils(){
    }

    /**
     * 拍照
     */
    public YPicUtils addCamera(Context context, List<LocalMedia> selectData, OnResultListener listener) {
        YPermissionUtil.requestCamera((FragmentActivity) context,"", new YPermissionUtil.OnPermissionListener() {
            @Override
            public void onSuccess() {
                PictureSelector.create(context)
                        .openCamera(SelectMimeType.ofImage())
                        .setCompressEngine(new CompressEngine())//压缩处理
                        .setSelectedData(selectData)//已选图片
                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                            @Override
                            public void onResult(ArrayList<LocalMedia> result) {
                                if (listener != null){
                                    listener.onResultListener(result);
                                }
                            }

                            @Override
                            public void onCancel() {
                            }
                        });
            }

            @Override
            public void onError() {
            }
        });
        return this;
    }

    /**
     * 水印拍照
     */
    public YPicUtils addWaterMask(Context context, List<LocalMedia> selectData, WatermarkEntity waterMaskBean, OnResultListener listener) {
        YPermissionUtil.requestCamera((FragmentActivity) context, "", new YPermissionUtil.OnPermissionListener() {
            @Override
            public void onSuccess() {
                PictureSelector.create(context)
                        .openCamera(SelectMimeType.ofImage())
                        .setCompressEngine(new CompressEngine())//压缩处理
                        .setAddBitmapWatermarkListener(new WatermarkEventListener(waterMaskBean))//水印处理
                        .setSelectedData(selectData)//已选图片
                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                            @Override
                            public void onResult(ArrayList<LocalMedia> result) {
                                if (listener != null){
                                    listener.onResultListener(result);
                                }
                            }

                            @Override
                            public void onCancel() {
                            }
                        });
            }

            @Override
            public void onError() {

            }
        });

        return this;
    }

    /**
     * 相册
     */
    public YPicUtils addAlbum(Context context, OnResultListener listener) {
        YPermissionUtil.requestCamera((FragmentActivity) context, "", new YPermissionUtil.OnPermissionListener() {
            @Override
            public void onSuccess() {
                PictureSelector.create(context)
                        .openGallery(SelectMimeType.ofAll())
                        .setImageEngine(GlideEngine.createGlideEngine())
                        .setCompressEngine(new CompressEngine())//压缩处理
                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                            @Override
                            public void onResult(ArrayList<LocalMedia> result) {
                                if (listener != null){
                                    listener.onResultListener(result);
                                }
                            }

                            @Override
                            public void onCancel() {
                            }
                        });
            }

            @Override
            public void onError() {

            }
        });
        return this;
    }

    /**
     * 相册(单张)
     */
    public YPicUtils addAlbumSingle(Context context, String content, OnResultListener listener) {
        YPermissionUtil.requestCamera((FragmentActivity) context, content, new YPermissionUtil.OnPermissionListener() {
            @Override
            public void onSuccess() {
                PictureSelector.create(context)
                        .openGallery(SelectMimeType.ofAll())
                        .setImageEngine(GlideEngine.createGlideEngine())
                        .setCompressEngine(new CompressEngine())//压缩处理
                        .setMaxSelectNum(1)
                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                            @Override
                            public void onResult(ArrayList<LocalMedia> result) {
                                if (listener != null){
                                    listener.onResultListener(result);
                                }
                            }

                            @Override
                            public void onCancel() {
                            }
                        });
            }

            @Override
            public void onError() {
            }
        });
        return this;
    }

    /**
     * 相册（9）或视频（1）
     */
    public YPicUtils addAlbumAndVideo(Context context, String content, SelectPicAdapter adapter, OnResultListener listener) {
        YPermissionUtil.requestCamera((FragmentActivity) context, content, new YPermissionUtil.OnPermissionListener() {
            @Override
            public void onSuccess() {
                int max = 9;
                List<SelectPicBean> list = adapter.getData();
                int chooseMode = SelectMimeType.ofAll();
                if (YCollectionUtil.isNotEmpty(list)){
                    for (SelectPicBean item: list) {
                        if (YFileUtil.isVideo(item.getPic())){
                            YToastUtil.showShort("只能上传一个视频");
                            return;
                        }
                    }
                    chooseMode = SelectMimeType.ofImage();
                    max -= list.size();
                }
                PictureSelector.create(context)
                        .openGallery(chooseMode)
                        .setImageEngine(GlideEngine.createGlideEngine())
                        .setCompressEngine(new CompressEngine())//压缩处理
                        .setMaxSelectNum(max)
                        .setMaxVideoSelectNum(1)
                        .setRecordVideoMaxSecond(15)//视频只能录制15s
                        .setFilterVideoMaxSecond(15)//过滤超过15s视频
                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                            @Override
                            public void onResult(ArrayList<LocalMedia> result) {
                                if (listener != null){
                                    listener.onResultListener(result);
                                }
                            }

                            @Override
                            public void onCancel() {
                            }
                        });
            }

            @Override
            public void onError() {

            }
        });
        return this;
    }

    /**
     * 添加头像
     */
    public YPicUtils addAvatar(Context context, OnResultListener listener) {
        YPermissionUtil.requestCamera((FragmentActivity) context, "申请拍照权限：用于上传个人头像",  new YPermissionUtil.OnPermissionListener() {
            @Override
            public void onSuccess() {
                PictureSelector.create(context)
                        .openGallery(SelectMimeType.ofImage())
                        .setImageEngine(GlideEngine.createGlideEngine())
                        .setSelectionMode(SelectModeConfig.MULTIPLE)
                        .isDisplayCamera(true)
                        .setMaxSelectNum(1)
                        .setCompressEngine(new CompressEngine())
                        .setCropEngine(new ImageCropEngine(context, 1, 1))

                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                            @Override
                            public void onResult(ArrayList<LocalMedia> result) {
                                if (listener != null){
                                    listener.onResultListener(result);
                                }
                            }

                            @Override
                            public void onCancel() {
                            }
                        });
            }

            @Override
            public void onError() {
            }
        });

        return this;
    }


    public void zoomPic(Context context, List<String> list, int position){
        //放大图片
        ArrayList<LocalMedia> localMedias = new ArrayList<>();
        for (String item: list) {
            LocalMedia media = new LocalMedia();
            if (YFileUtil.isVideo(item)){
                media.setMimeType("video/mp4");
                media.setRealPath(item);
            }
            media.setPath(item);
            localMedias.add(media);
        }
        YPicUtils.getInstance().zoomPic(context, localMedias, position);
    }

    public void zoomPic(Context context, ArrayList<LocalMedia> list, int position){
        PictureSelector.create(context)
                .openPreview()
                .setImageEngine(GlideEngine.createGlideEngine())
                .setExternalPreviewEventListener(new OnExternalPreviewEventListener() {
                    @Override
                    public void onPreviewDelete(int position) {
                        //删除图片
                    }

                    @Override
                    public boolean onLongPressDownload(Context context, LocalMedia media) {
                        return false;
                    }

                })
                .startActivityPreview(position, false, list);
    }

}
